package Dao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;

public class Buy extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ArrayList<Integer> all_number_arrayList=new ArrayList<>();
        ArrayList<String> buy_name_arraylist=new ArrayList<>();
        ArrayList<Double> buy_price_arrayList=new ArrayList<>();
        ArrayList<Integer> id_arraylist=new ArrayList<>();
        double Zong_Jia=0;
        Connection connection;
        String sql,shopcar_sql,str_data_shopcar="";
        PreparedStatement  preparedStatement;
        ResultSet resultSet;
        HttpSession httpSession=req.getSession();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/it?useUnicode=true&characterEncoding=utf8","root","123456");
            sql="select * from shop";
            preparedStatement=connection.prepareStatement(sql);
            resultSet=preparedStatement.executeQuery();
            while(resultSet.next()){
                String linshi_name=resultSet.getString("name");
                double linshi_price=resultSet.getDouble("price");
                int linshi_number=resultSet.getInt("number");
                int linshi_id=resultSet.getInt("id");
                buy_name_arraylist.add(linshi_name);
                buy_price_arrayList.add(linshi_price);
                all_number_arrayList.add(linshi_number);
                id_arraylist.add(linshi_id);
            }
            //总量该，购物车该
            for(int i=0;i<buy_name_arraylist.size();i++){

                System.out.print("购买物品 "+buy_name_arraylist.get(i));
                str_data_shopcar=str_data_shopcar+buy_name_arraylist.get(i)+",";

                System.out.print(" 购买价格"+buy_price_arrayList.get(i));
                double all_price=buy_price_arrayList.get(i);
                str_data_shopcar=str_data_shopcar+buy_price_arrayList.get(i)+",";

                int linshi_buy_number= Integer.parseInt(req.getParameter("buy_number_"+id_arraylist.get(i)));
                System.out.print(" 购买数量"+linshi_buy_number);
                str_data_shopcar=str_data_shopcar+linshi_buy_number+",*";
                int all_number= all_number_arrayList.get(i)-linshi_buy_number;

                //商品
                System.out.println(" 剩余数量"+all_number);
                sql="update shop set number ="+all_number+" where id ="+id_arraylist.get(i);
                preparedStatement=connection.prepareStatement(sql);
                preparedStatement.executeUpdate();
                double linshi_price=buy_price_arrayList.get(i);
                Zong_Jia=Zong_Jia+linshi_price*linshi_buy_number;
            }
            System.out.println("购买总价格"+Zong_Jia);

//            放入数据库里
            String name= (String) httpSession.getAttribute("name");
            System.out.println("名称："+name);
            shopcar_sql="update login set buy_name = ? where name = ?";
            preparedStatement=connection.prepareStatement(shopcar_sql);
            preparedStatement.setString(1,str_data_shopcar);
            preparedStatement.setString(2,name);
            preparedStatement.executeUpdate();
            shopcar_sql="update login set zong_price = ? where name = ?";
            preparedStatement=connection.prepareStatement(shopcar_sql);
            preparedStatement.setDouble(1,Zong_Jia);
            preparedStatement.setString(2,name);
            preparedStatement.executeUpdate();
            resp.sendRedirect("shopcar.jsp");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
}
